import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from './router'
import App from './App.vue'
import './style.css'

// 导入状态管理
import { useAuthStore, useAppStore } from '@/stores'

const app = createApp(App)
const pinia = createPinia()

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(pinia)
app.use(router)
app.use(ElementPlus)

// 先挂载应用
app.mount('#app')

// 在应用挂载后初始化状态（使用错误边界）
try {
  const authStore = useAuthStore()
  const appStore = useAppStore()

  // 异步初始化认证状态和应用设置
  setTimeout(() => {
    Promise.all([
      authStore.initializeAuth(),
      appStore.initializeTheme()
    ]).catch(error => {
      console.error('Failed to initialize app:', error)
    })
  }, 100)
} catch (error) {
  console.error('Failed to initialize stores:', error)
}